<template>
  <div>
    <el-form :model="queryParam" :inline=true style="display: flex">
      <el-form-item label="批号">
        <el-input v-model="queryParam.batchNo" style="width:160px"/>
      </el-form-item>
      <el-form-item label="配置id">
        <el-input v-model="queryParam.confId" style="width:160px"/>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParam.status" placeholder="请选择" :clearable=true style="width:160px">
          <el-option v-for="t in validStatus" :label="t.name" :value="t.value" :key="t.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="请求参数">
        <el-input v-model="queryParam.request" style="width:160px"/>
      </el-form-item>
      <el-form-item label="响应参数">
        <el-input v-model="queryParam.response" style="width:160px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryPage">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="producerSourceData">触发生成数据</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
      <el-table-column prop="id" label="序号" width="130"></el-table-column>
      <el-table-column prop="batchNo" label="批号" width="130"></el-table-column>
      <el-table-column prop="confId" label="配置id" width="100"></el-table-column>
      <el-table-column
        label="事件名称"
        width="120">
        <template slot-scope="scope">
          <el-button @click="requestEvent(scope.row)" type="text">{{ scope.row.eventName }}-{{
              scope.row.stepName
            }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="60">
        <template slot-scope="scope">
          <span>{{ value2Name(validStatus, scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="exeStatus" label="exe状态" width="80">
        <template slot-scope="scope">
          <span>{{ value2Name(exeStatus, scope.row.exeStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="request" label="请求参数">
        <template slot-scope="scope">
          <div class="typeEnter">{{ scope.row.request  | ellipsis }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="response" label="响应参数" width="300">
        <template slot-scope="scope">
          <div class="typeEnter">{{ scope.row.response  | ellipsis }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="pageTotal>0"
      :total="pageTotal"
      :page.sync="queryParam.pageNum"
      :limit.sync="queryParam.pageSize"
      @pagination="queryPage"
    />
  </div>
</template>

<script>
import store from "../../../../store";
import {ellipsis, parseTime, value2Name} from "../../../../utils";

export default {
  data() {
    return {
      // 查询参数
      pageTotal: 0,
      queryParam: {
        batchNo: '',
        confId: '',
        status: '',
        request: '',
        response: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      validStatus: store.listDictionary('isValid'),
      exeStatus: store.listDictionary('exeStatus'),
      colorkeys: {}
    }
  },
  mounted() {
    this.pageList()
  },
  methods: {
    value2Name,
    ellipsis,
    parseTime,
    requestEvent(row) {
      let _this = this
      let param = {
        id: row.id
      }
      this.$api.trigerByDataId(param).then(res => {
        if (res.code === '00000') {
          row.response = res.data
          row.exeStatus = 1
          _this.$message("触发完毕--->" + row.eventName + "--->" + row.stepName)
        } else {
          _this.$message(res.msg)
        }
      }).catch(e => {
        console.log('接口访问异常!')
      })
    },
    queryPage() {
      let _this = this
      _this.pageList()
    },
    pageList() {
      var _this = this
      this.$api.trigerDataPageList(_this.queryParam).then(res => {
        if (res.code === '00000') {
          _this.tableData = res.data.pageList
          _this.pageTotal = res.data.pageTotal

          _this.tableData.forEach(e => this.colorkeys[e.batchNo] = 1)
        } else {
          _this.$message(res.msg)
        }
      }).catch(e => {
        console.log('接口访问异常!')
      })
    },
    //只接收一个参数,内部名字是映射
    tableRowClassName({row, rowIndex}) {
      let colorsize = 5
      let keyArr = Object.getOwnPropertyNames(this.colorkeys)
      let len = keyArr.length
      for (let i = 0; i < len; i++) {
        this.colorkeys[keyArr[i]] = i % colorsize
      }

      let n = this.colorkeys[row['batchNo']];
      if (n < colorsize) {
        return "row-color-" + n;
      } else {
        return '';
      }
    },
    producerSourceData() {
      let _this = this
      this.$api.trigerProducerSourceData().then(res => {
        if (res.code === '00000') {
          _this.$message("触发生成数据成功")
        } else {
          _this.$message(res.msg)
        }
      }).catch(e => {
        console.log('接口访问异常!')
      })
    }
  },
  filters: {
    ellipsis: (str) => ellipsis(str, 300)
  }
}
</script>

<style>
.el-table .row-color-0 {
  background: #8cc5ff;
}

.el-table .row-color-1 {
  background: #cf9236;
}

.el-table .row-color-2 {
  background: #85ce61;
}

.el-table .row-color-3 {
  background: #f78989;
}

.el-table .row-color-4 {
  background: #ebb563;
}
</style>

